package com.sentiment.strategy;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

public class WordSegment {
	public static Map<String, Integer> wordCount(String text) {
		List<String> li = segmentCore(text);
		Map<String, Integer> map = new HashMap<String, Integer>();
		for (String str : li) {
			if (map.containsKey(str)) {
				Integer val = map.get(str);
				map.replace(str, val + 1);
			} else {
				map.put(str, 1);
			}
		}
		return map;
	}

	public static List<String> segmentCore(String text) {
		IKSegmenter ikSegmenter = new IKSegmenter(new StringReader(text), true);
		Lexeme lexeme = null;
		List<String> li = new ArrayList<String>();
		try {
			while ((lexeme = ikSegmenter.next()) != null) {
				li.add(lexeme.getLexemeText());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return li;
	}
	
	public static void main(String[] args){
		File fl = new File("C:/Users/wjk/Desktop/新建文件夹/srcTxtFiles/Js实现异步刷新(原创).txt");
		try{
			BufferedReader br = new BufferedReader(new FileReader(fl));
			StringBuilder sb = new StringBuilder();
			String temp;
			while((temp = br.readLine())!=null){
				sb.append(temp);
			}
			Map<String,Integer> map = wordCount(sb.toString());
			System.out.println(map);
		}catch(IOException e){
			e.printStackTrace();
		}
	}
}
